package com.microweb.toastmaster.webapp.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import com.microweb.toastmaster.model.Speech;
import com.microweb.toastmaster.model.TMProject;
import com.microweb.toastmaster.model.User;
import com.microweb.toastmaster.service.SpeechManager;
import com.microweb.toastmaster.service.UserManager;
import com.microweb.toastmaster.service.TMProjectManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

public class SpeechFormController extends BaseFormController {
    private SpeechManager speechManager = null;
    private UserManager userManager = null;
    private TMProjectManager TMProjectManager = null;

    public void setSpeechManager(SpeechManager speechManager) {
        this.speechManager = speechManager;
    }
    
    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
    
    public void setTMProjectManager(TMProjectManager TMProjectManager) {
        this.TMProjectManager = TMProjectManager;
    }
    
    public SpeechFormController() {
        setCommandName("speech");
        setCommandClass(Speech.class);
    }

    protected Object formBackingObject(HttpServletRequest request)
    throws Exception {
        String id = request.getParameter("id");
        Speech speech = null;

        if (!StringUtils.isEmpty(id)) {
            speech = speechManager.getSpeech(id);
        } else {
            speech = new Speech();
        }

        return speech;
    }

    public ModelAndView onSubmit(HttpServletRequest request,
                                 HttpServletResponse response, Object command,
                                 BindException errors)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'onSubmit' method...");
        }

        Speech speech = (Speech) command;
        boolean isNew = (speech.getId() == null);
        Locale locale = request.getLocale();

        if (request.getParameter("delete") != null) {
            speechManager.removeSpeech(speech.getId().toString());

            saveMessage(request, getText("speech.deleted", locale));
        } else {
	    fillSpeech(request, speech);
            speechManager.saveSpeech(speech);

            String key = (isNew) ? "speech.added" : "speech.updated";
            saveMessage(request, getText(key, locale));

            if (!isNew) {
                return new ModelAndView("redirect:editSpeech.html", "id", speech.getId());
            }
        }

        return new ModelAndView(getSuccessView());
    }
    
    protected void fillSpeech(HttpServletRequest request, Speech speech) {
	String speaker_id = request.getParameter("speaker_id");
	if(speaker_id != null && speaker_id.trim().length() > 0 ) {
	    User speaker = userManager.getUser(speaker_id);
	    speech.setUser(speaker);
	}
	
	String project_id = request.getParameter("project_id");
	if(project_id != null && project_id.trim().length() > 0 ) {
	    TMProject project = TMProjectManager.getTMProject(project_id);
	    speech.setTMProject(project);
	}
    }
    
       protected Map referenceData(HttpServletRequest request) throws Exception {  
	 HashMap referenceData = new HashMap();  
	 List users = userManager.getUsers(null);
	 referenceData.put("availableUsers",users);
	 
	 List tmprojects = TMProjectManager.getTMProjects(null);
	 referenceData.put("availableProjects",tmprojects);
         return referenceData;  
     }  
       
}
